package com.mostromas.ess.product.controller;

import com.mostromas.ess.entities.ProductSize;
import com.mostromas.ess.product.req.ProductSizeREQ;
import com.mostromas.ess.product.service.IProductSizeService;
import com.mostromas.ess.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 商品尺寸表-前端控制器
 */
@Api(value = "商品尺码管理接口",description = "尺码管理接口，提供尺码的增、删、改、查")
@RestController //所有方法都会返回json字符串进行相应
@RequestMapping("/size")
public class ProductSizeController {

    @Autowired
    private IProductSizeService productSizeService;

    /**
     * 根据分类ID查询尺码列表
     * @param req
     * @return
     */
    @PreAuthorize(("hasAuthority('size:search')"))
    @ApiOperation("根据商品尺码分类ID查询尺码列表接口")
    @PostMapping("/search") //请求路径： /product/size/search
    public Result search(@RequestBody ProductSizeREQ req){
        return productSizeService.queryPage(req);
    }

    @PreAuthorize(("hasAuthority('size:search')"))
    @ApiOperation("查询商品尺码详情接口")
    @ApiImplicitParam(name = "id",value = "尺码ID",required = true)
    @GetMapping("/{id}") //请求路径：/product/size/{id}
    public Result view(@PathVariable("id") String id){
        ProductSize productSize = productSizeService.getById(id);
        return Result.ok(productSize);
    }

    @PreAuthorize(("hasAuthority('size:edit')"))
    @ApiOperation("修改商品尺码信息接口")
    @PutMapping //put方式调用接口：/product/size
    public Result update(@RequestBody ProductSize productSize){
        productSizeService.updateById(productSize);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('size:add')"))
    @ApiOperation("新增商品尺码信息接口")
    @PostMapping //post方式调用接口：/product/size
    public Result save(@RequestBody ProductSize productSize){
        productSizeService.save(productSize);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('size:delete')"))
    @ApiOperation("删除商品尺码信息接口")
    @ApiImplicitParam(name = "id",value = "尺码ID",required = true)
    @DeleteMapping("/{id}") //delete方式调用接口：/product/size/{id}
    public Result delete(@PathVariable("id") String id){
        productSizeService.removeById(id);
        return Result.ok();
    }

    @PreAuthorize(("hasAuthority('size:search')"))
    @ApiOperation("获取所有的商品尺码信息接口")
    @GetMapping("/list") //请求路径：/product/size/list
    public Result list(){
        return productSizeService.selectAll();
    }
}
